<?php

namespace Xuan\Controller;

use Think\Controller;

class AjaxController extends Controller {
    public function getOrder() {
      $order =   M("order")->field("id")->where("is_read = 0")->find();
      if($order){
          M("order")->where("id = " . $order['id'] . "")->save(array("is_read" => '1'));
          echo $order['id'];
      }
      
        
    }
    public function statics_set() {
        $control = I("post.control");
        $mod = I("post.mod");
        $names = I("post.names");
        $vals = I("post.vals");
        if ($names) {
            $namesArr = explode(",", substr($names, 0, -1));
            $valsArr = explode(",", substr($vals, 0, -1));
            $url_other = '';
            foreach ($namesArr as $k => $v) {
                if ($valsArr[$k] > 0) {
                    $url_other .= "/" . $v . "/" . $valsArr[$k];
                }
            }
            M("admin_menu2")->where("control = '" . $control . "' AND `mod` = '" . $mod . "'")->save(array("url_other" => $url_other));
            echo __APP__ . "/" . MODULE_NAME . "/" . $control . "/" . $mod . $url_other;
        }
    }

    public function delPic() {
        $id = I("post.id");
        $table = I("post.table");
        $field = I("post.field");
        M($table)->where("id = " . $id . "")->save(array($field => ''));
    }

    public function sendEmail() {
        $email = I("post.email");
        $title = I("post.title");
        $content = I("post.content");
        $config = getTableConfig();
        $rs = sendMail($email, $config['title'], $title, $content);
        if ($rs != 1) {
            $rs = "邮箱发送失败，请检查邮箱配置！";
        }
        echo $rs;
    }

    public function sendShortMessagesConfirm() {
        $phone = I("post.phone");
        $email = I("post.email");
        $content = I("post.content");
        $is_send_phone = I("post.is_send_phone");
        $is_send_email = I("post.is_send_email");
        $i = 0;
        $word = '';
        if ($is_send_phone == 'true') {
            $state = sendMobile($phone, $content, 'send_suc');
            if ($state == 1) {
                $i++;
            } else {
                $word .= getMessageResult($state);
            }
        }

        if ($is_send_email == 'true') {
            $state = sendEmail($email, $content, 'send_suc');
            if ($state != 1) {
                $word .= "邮箱发送失败，请检查邮箱配置！";
            } else {
                $i++;
            }
        }
        if (!$word) {
            echo $i;
        } else {
            echo $word;
        }
    }

    public function sendShortMessages() {
        $id = I("post.id", 0, 'int');
        $arr['content'] = changeOrderContent($id, 'send_suc', 'email');
        $info = M("three")->where("id = 1")->find();
        $short = json_decode($info['shortMessage'], true);
        $email = json_decode($info['emails'], true);
        $arr['is_send_phone'] = $short['is_start'];
        $arr['is_send_email'] = $email['is_start'];
        echo json_encode($arr);
    }

    public function checkPwd() {
        $pwdOld = md5(I('post.pwdOld'));
        $info = M("admin_user")->field("id")->where("id = '" . session('admin_uid') . "' AND pwd = '" . $pwdOld . "'")->find();
        // echo $Dao->getLastSql();
        if (empty($info)) {
            echo "false";
        } else {
            echo "true";
        }
    }

    public function checkLogin() {
        $name = I('post.name');
        $pwd = md5(I('post.pwd'));
        $info = M("admin_user")->field("id,action")->where("name='" . $name . "' AND pwd = '" . $pwd . "'")->find();
//        echo M("admin_user")->getLastSql();
        if ($info) {
            if ($info['action']) {
                $menu = M("admin_menu")->field("control,mod")->where("id in (" . $info['action'] . ") AND pid !=0 AND control !=''")->find();
            }
            session('admin_uid', $info['id']);
            session('admin_name', $name);
            session('admin_action', $info['action']);
            M("admin_user")->where("id = " . $info['id'] . "")->save(array("last_login" => time(), "last_ip" => getIP()));
            echo $info['id'];
//             if($info['id'] == 1){
//                 echo U("Index/info");
//             }else{
//                 echo U($menu['control']."/".$menu['mod']);
//             }
        } else {
            echo -1;
        }
    }

    public function changeOrdConfirm() {
        $table = I("post.table");
        $id = I("post.id");
        $data['ord'] = I("post.ord", 30, 'int');
        M($table)->where("id = " . $id . "")->save($data);
        isClearTempFile($table);
    }

    public function addGiftsCatConfirm() {
        $data['title'] = I("post.name");
        $data['ord'] = 30;
        $table = I("post.table");
        $lastid = M($table)->add($data);
        echo $lastid;
    }

    public function addGoodsCatConfirm() {
        $data['sid'] = I("post.id", '0', 'int');
        if ($data['sid'] > 0) {
            $data['name'] = I("post.name");
            $data['is_check'] = 1;
            $data['ord'] = 30;
            echo M("goods_cat")->add($data);
        }
    }

    public function getGoodsCat() {
        $id = I("post.id", '0', 'int');
        $lists = M("goods_cat")->where("sid = " . $id . "")->select();
        echo json_encode($lists);
    }

    public function more_del() {
        if (session("admin_uid") > 0) {
            $ids = I("post.ids");
            if (!empty($ids)) {
                $ids = explode(",", $ids);
                $table = I("post.table");
                foreach ($ids as $v) {
                    M($table)->where("id = " . $v . "")->limit(1)->delete();
                    if ($table == 'order') {
                        M("order_goods")->where("pid = " . $v . "")->delete();
                    }
                }
                isClearTempFile($table);
            }
        }
    }

    public function yes() {
        $id = I("post.id", '0', 'int');
        $field = I("post.field") ? I("post.field") : "is_check";
        $table = I('post.table');
        $info = M($table)->where("id = " . $id . "")->find();
        if ($info[$field] == 1) {
            $data[$field] = 0;
            echo $word = '否';
        } else {
            $data[$field] = 1;
            echo $word = '是';
        }
        M($table)->where("id = " . $id . "")->save($data);
        clearTempFile();
    }

    public function getArea() {
        $id = I("post.id");
        $table = I("post.table") ? I("post.table") : "area";
        if ($id != '') {
            $areas = M($table)->where("pid = " . $id . "")->select();
//            echo M($table)->getlastsql();
            foreach ($areas as $v) {
                echo "<option value=" . $v['id'] . ">" . $v['name'] . "</option>";
            }
        }
    }

    public function checkSubArea() {
        $id = I("post.id", '', 'int');
        $Dao = M('area');
        $lists = $Dao->field("name")->where("pid = " . $id . "")->select();
        if ($lists) {
            $names = '';
            foreach ($lists as $v) {
                $names .= $v['name'] . ",";
            }
            $names = "[" . substr($names, 0, -1) . "]";
            echo $names;
        }
    }

    public $str = '';

    public function getTopAreaIds($id) {
        $detail = M("area")->where("id = " . $id . "")->find();
        if ($detail) {
            $this->str .= $detail['id'] . ",";
            $this->getTopAreaIds($detail['pid']);
        }
    }

    public function getTopAreaIds2() {
        $id = I("post.id");
        $this->getTopAreaIds($id);
        if ($this->str) {
            $str = substr($this->str, 0, -1);
            echo $str;
        }
    }

    public function checkSchoolCircle() {
        $searchId = I("post.searchId");
        $info = M("area")->field("id")->where("id = " . $searchId . "")->find();
        echo $info['id'];
    }

    public function check_name() {
        $name = I('post.name');
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $sql = " AND id != " . $id . "";
        }
        $info = M("user")->where("name='" . $name . "' " . $sql . "")->find();
        if (empty($info)) {
            echo "true";
        } else {
            echo "false";
        }
    }

    public function check_admin_name() {
        $name = I('post.name');
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $sql = " AND id != " . $id . "";
        }
        $info = M("admin_user")->where("name='" . $name . "' " . $sql . "")->find();
        if (empty($info)) {
            echo "true";
        } else {
            echo "false";
        }
    }

    public function check_email() {
        $email = I('post.email');
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $sql = " AND id != " . $id . "";
        }
        $info = M("user")->where("email='" . $email . "' " . $sql . "")->find();
        if (empty($info)) {
            echo "true";
        } else {
            echo "false";
        }
    }

    public function check_mobile() {
        $phone = I('post.phone');
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $sql = " AND id != " . $id . "";
        }
        $info = M("user")->where("phone='" . $phone . "' " . $sql . "")->find();
        if (empty($info)) {
            echo "true";
        } else {
            echo "false";
        }
    }

    public function clearTempFile() {
        clearTempFile();
    }

    public function goOrderUrl() {
        $year = I("post.year", 0, 'int');
        $month = I("post.month", 0, 'int');
        $day = I("post.day", 0, 'int');
        $type = I("post.type", 0, 'int');
        $mtype = I("post.mtype");
        $shop_id = I("post.shop_id", 0, 'int');
        $arr = array("year" => $year, "month" => $month, "day" => $day, "type" => $type, "mtype" => $mtype, "shop_id" => $shop_id);
        foreach ($arr as $k => $v) {
            if ($v == '') {
                unset($arr[$k]);
            }
        }
        echo U("Statics/" . $mtype . "", $arr);
    }

    public function yearChange() {
        $year = I("post.year", 0, 'int');
        $month = I("post.month", 0, 'int');
        $select = '<option value="0">请选择月份</option>';
        for ($i = 1; $i <= 12; $i++) {
            $select .= "<option value=" . $i . ">" . $i . "月</option>";
        }
        echo $select;
    }

    public function monthChange() {
        $year = I("post.year", 0, 'int');
        $month = I("post.month", 0, 'int');
        $day_max = getMonthLastDay($month, $year);
        $select = '<option value="0">请选择日</option>';
        for ($i = 1; $i <= $day_max; $i++) {
            $select .= "<option value=" . $i . ">" . $i . "日</option>";
        }
        echo $select;
    }

}

?>
